package com.jerry.huihome.module.find.fragment;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.adapter.CommentAdapter;
import com.jerry.huihome.module.find.entity.WordOfMouth;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/12.
 */
public class MouthOfWordFragment extends BaseFragment {

    @BindView(R.id.tv_level_score)
    TextView tvLevelScore;
    @BindView(R.id.tv_level_average)
    TextView tvLevelAverage;
    @BindView(R.id.tv_attitude_score)
    TextView tvAttitudeScore;
    @BindView(R.id.tv_attitude_average)
    TextView tvAttitudeAverage;
    @BindView(R.id.tv_active_score)
    TextView tvActiveScore;
    @BindView(R.id.tv_active_average)
    TextView tvActiveAverage;
    @BindView(R.id.tv_comment)
    TextView tvComment;
    @BindView(R.id.ptf_comment)
    PullToRefreshListView ptfComment;
    private String designerid;
    private int pageIndex = 1;
    private WordOfMouth wordOfMouth;
    private CommentAdapter adapter;
    private List<WordOfMouth.BodyBean.AssessItemsBean> assessItemsBeen = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_word_of_mouth;
    }

    @Override
    protected void getReqParams(Bundle bundle) {
        designerid = bundle.getString("designerid");
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void loadData() {
        String url = Urls.WORD_OF_MOUTH_URL + designerid + "&pageIndex=" + pageIndex;
        OkHttpUtils.get(url, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                if (data != null) {
                    if (pageIndex == 1) {
                        if (adapter != null){
                            adapter.clear();
                        }
                    }
                    Gson gson = new Gson();
                    wordOfMouth = gson.fromJson(data, WordOfMouth.class);
                    setViews();
                }
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });
    }

    private void setViews() {
        tvLevelScore.setText(wordOfMouth.getBody().getScoreProf() + "");
        tvAttitudeScore.setText(wordOfMouth.getBody().getScoreManner() + "");
        tvActiveScore.setText(wordOfMouth.getBody().getScoreActive() + "");
        double scoreProfDeta = wordOfMouth.getBody().getScoreProfDeta();
        if (scoreProfDeta > 0) {
            tvLevelAverage.setText("高于平均" + (int) (scoreProfDeta * 100));
        }
        if (scoreProfDeta < 0) {
            tvLevelAverage.setText("低于平均" + Math.abs((int) (scoreProfDeta * 100)));
        } else {
            tvLevelAverage.setText("与平均持平");
        }
        double scoreMannerDeta = wordOfMouth.getBody().getScoreMannerDeta();
        if (scoreMannerDeta > 0) {
            tvAttitudeAverage.setText("高于平均" + (int) (scoreMannerDeta * 100));
        }
        if (scoreMannerDeta < 0) {
            tvAttitudeAverage.setText("低于平均" + Math.abs((int) (scoreMannerDeta * 100)));
        } else {
            tvAttitudeAverage.setText("与平均持平");
        }
        Float scoreActiveDeta = wordOfMouth.getBody().getScoreActiveDeta();
        if (scoreActiveDeta > 0) {
            tvActiveAverage.setText("高于平均" + (int) (scoreActiveDeta * 100));
        }
        if (scoreActiveDeta < 0) {
            tvActiveAverage.setText("低于平均" + Math.abs((int) (scoreActiveDeta * 100)));
        } else {
            tvActiveAverage.setText("与平均持平");
        }
        tvComment.setText("评论" + wordOfMouth.getBody().getAssessCount());

        List<WordOfMouth.BodyBean.AssessItemsBean> assessItems = wordOfMouth.getBody().getAssessItems();
        if (assessItems != null) {
            assessItemsBeen.addAll(assessItems);
            showAdapter();
        }else {
            Toast.makeText(getContext(),"没有更多内容了",Toast.LENGTH_LONG).show();
        }

    }

    private void showAdapter() {
        adapter = new CommentAdapter(getContext(), assessItemsBeen);
        ptfComment.setAdapter(adapter);
        ptfComment.getRefreshableView().setDivider(null);
        ptfComment.getRefreshableView().setSelector(android.R.color.transparent);
        ptfComment.setMode(PullToRefreshBase.Mode.BOTH);
        ptfComment.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex++;
                loadData();
            }
        });
        ptfComment.onRefreshComplete();
    }
}
